package Ptolemaiosz;

import java.awt.*;
import java.util.ArrayList;

public class LevelGraphicalEntity{
	private ArrayList<LevelPartGraphicalEntity> levelPartGraphicalEntities = new ArrayList<LevelPartGraphicalEntity>();
	private Level level;
        
        //Ablak merete fix
        private int LevelPartMargin = 10;

        public void recalculateViewPorts()
            {
                levelPartGraphicalEntities = new ArrayList<LevelPartGraphicalEntity>();
                setLevel(level);
            }
        
	protected void draw(Graphics graphics)
            {
                //Megfelelo kasztolas, valamiert kell
                Graphics2D g2 = (Graphics2D)graphics;
                
                    //Hatter kirajzolasa                
                    g2.drawImage(TextureManager.getTexture("level"), 0, 0, View.width, View.height, null);
         
                
                //Minden egyes levelparton hivunk egy displayt
                for(int i = 0; i < levelPartGraphicalEntities.size(); i++ )
                    {
                        levelPartGraphicalEntities.get(i).draw(graphics);
                    }            
            }

	public void setLevel(Level level)
            {
		//Beallitjuk a level attributumot
                this.level = level;
                
                //Lekerdezzuk a levelpartok tombjet
                ArrayList<ArrayList<LevelPart>> neighbour2DArray = level.getneighbour2DArray();
               
                    //Sorok szamanak meghatarozasa, ami azert nehezebb, mert eredetileg 10 ueres sor van es a nem uresek szama kell
                    int numberOfRows = 0;
                
                    for(int i = 0;i < neighbour2DArray.size();i++)
                        {
                            if(neighbour2DArray.get(i).size() != 0)
                                numberOfRows++;
                        }
                
                    //Oszlopok szamanak a meghatarozasa
                    int numberOfCols = neighbour2DArray.get(0).size();


                    //Egy levelpart szelessegenek a meghatarozasa
                    int LevelPartWidth  = (View.width  - (numberOfCols * 2 * LevelPartMargin)) / numberOfCols; 
                    int LevelPartHeight = (View.height - (numberOfRows * 2 * LevelPartMargin)) / numberOfRows; 
          

                    //Minden levelpart letrehozasa es Viewport beallitasa
                    for(int row = 0; row < numberOfRows; row++)
                        {
                            for(int col = 0 ;col < numberOfCols; col++)
                            {
                                //ViewPort kiszamitasa
                                int paddignLeft = col * LevelPartWidth + LevelPartMargin + col * 2 * LevelPartMargin;
                                int paddignTop  = row * LevelPartHeight + LevelPartMargin + row * 2 * LevelPartMargin;
                                
                                ArrayList<Integer> viewPort = new ArrayList<Integer>();
                                        viewPort.add(paddignLeft);
                                        viewPort.add(paddignTop);
                                        viewPort.add(LevelPartWidth);
                                        viewPort.add(LevelPartHeight);
                                
                                //Letrehozas
                                LevelPartGraphicalEntity currentGrahphycalEntity = new LevelPartGraphicalEntity();
                                    currentGrahphycalEntity.setLevelPart(neighbour2DArray.get(row).get(col));
                                    currentGrahphycalEntity.setViewPort(viewPort);
                                    currentGrahphycalEntity.setAvatars(level.getAvatars());
                                    
                                    
                                    
                                //Eltarolas
                                    levelPartGraphicalEntities.add(currentGrahphycalEntity);
                            }
                        }     
            }
}